package com.chtw.oasystem.service.impl;

import cn.hutool.core.util.IdUtil;
import com.chtw.oasystem.bean.ChangeLogs;
import com.chtw.oasystem.bean.Depts;
import com.chtw.oasystem.bean.DeptsExample;
import com.chtw.oasystem.bean.FullUserInfo;
import com.chtw.oasystem.common.changelogs.ChangeLogsUtils;
import com.chtw.oasystem.mapper.DeptsMapper;
import com.chtw.oasystem.respBean.DeptTree;
import com.chtw.oasystem.service.DeptService;
import com.chtw.oasystem.utils.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chtw
 * @date 2020-11-18 22:29
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptsMapper deptsMapper;

    @Autowired
    private ChangeLogServiceImpl changeLogService;

    @Override
    public List<Depts> list() {
        return deptsMapper.selectByExample(new DeptsExample());
    }

    @Override
    public boolean create(HttpSession session, Depts depts) {
        try {
            FullUserInfo user = Session.getCurrentUser(session);
            if (user == null) {
                throw new RuntimeException("用户未登录！");
            } else if (user.getRoleId() < 1) {
                if (depts.getParentId().equals("")) {
                    throw new RuntimeException("请选择父级部门");
                }
                if (depts.getDeptName().equals("")) {
                    throw new RuntimeException("请输入部门名称");
                }
                depts.setDeptId(IdUtil.simpleUUID());
                int isCreate = deptsMapper.insert(depts);
                if (isCreate > 0) {
                    ChangeLogs changeLog = ChangeLogsUtils.generateDeptChangeLog(user.getUserName(), user.getUserName(), "添加");
                    changeLogService.saveChangeLog(changeLog);
                    return true;
                }
                return false;
            } else {
                throw new RuntimeException("没有权限！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public boolean edit(HttpSession session, Depts depts) {
        try {
            FullUserInfo user = Session.getCurrentUser(session);
            if (user == null) {
                throw new RuntimeException("用户未登录！");
            } else if (user.getRoleId() < 1) {
                if (depts.getParentId().equals("")) {
                    throw new RuntimeException("请选择父级部门");
                }
                if (depts.getDeptName().equals("")) {
                    throw new RuntimeException("请输入部门名称");
                }
                if (depts.getDeptId().equals("")) {
                    throw new RuntimeException("请输入部门ID");
                }
                DeptsExample deptsExample = new DeptsExample();
                DeptsExample.Criteria re = deptsExample.createCriteria();
                re.andDeptIdEqualTo(depts.getDeptId());
                int isEdit = deptsMapper.updateByExampleSelective(depts, deptsExample);
                if (isEdit > 0) {
                    ChangeLogs changeLog = ChangeLogsUtils.generateDeptChangeLog(user.getUserName(), user.getUserName(), "编辑");
                    changeLogService.saveChangeLog(changeLog);
                    return true;
                }
                return false;
            } else {
                throw new RuntimeException("没有权限！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public boolean deleteDept(HttpSession session, String deptId) {
        try {
            FullUserInfo user = Session.getCurrentUser(session);
            if (user == null) {
                throw new RuntimeException("用户未登录！");
            } else if (user.getRoleId() < 1) {
                if (deptId.equals("")) {
                    throw new RuntimeException("请输入部门ID");
                }
                DeptsExample deptsExample = new DeptsExample();
                DeptsExample.Criteria re = deptsExample.createCriteria();
                re.andDeptIdEqualTo(deptId);
                int isDel = deptsMapper.deleteByExample(deptsExample);
                if (isDel > 0) {
                    ChangeLogs changeLog = ChangeLogsUtils.generateDeptChangeLog(user.getUserName(), user.getUserName(), "删除");
                    changeLogService.saveChangeLog(changeLog);
                    return true;
                }
                return false;
            } else {
                throw new RuntimeException("没有权限！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public Depts info(String deptId) {
        try {
            if (deptId.equals("")) {
                throw new RuntimeException("请选择部门");
            }
            DeptsExample example = new DeptsExample();
            DeptsExample.Criteria re = example.createCriteria();
            re.andDeptIdEqualTo(deptId);
            List<Depts> depts = deptsMapper.selectByExample(example);
            if (depts.size() > 0) {
                return depts.get(0);
            }
            throw new RuntimeException("未查询到相关部门");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    private List<DeptTree> search(List<Depts> depts, String searchId) {
        List<DeptTree> trees = new ArrayList<>();
        for (Depts dept : depts) {
            if (dept.getParentId().equals(searchId)) {
                DeptTree item = new DeptTree();
                item.setDeptId(dept.getDeptId());
                item.setDeptName(dept.getDeptName());
                item.setParentId(dept.getParentId());
                item.setId(dept.getId());
                item.setChild(this.search(depts, dept.getDeptId()));
                trees.add(item);
            }
        }
        return trees;
    }

    @Override
    public List<DeptTree> tree() {
        // 找根部门  paren_id = 0
        return this.search(this.list(), "0");
    }

    public List<String> getOwnerDeptIds(String deptId) {
        List<DeptTree> trees = this.search(this.list(), deptId);
        List<String> depts = new ArrayList<>();
        for (DeptTree tree : trees) {
            depts.add(tree.getDeptId());
            for (DeptTree deptTree : tree.getChild()) {
                depts.addAll(getOwnerDeptIds(deptTree.getDeptId()));
            }
        }
        return depts;
    }
}
